//
//  BS_NavigationController.swift
//  SWIFT_BAISI
//
//  Created by youxin on 2018/1/19.
//  Copyright © 2018年 YST. All rights reserved.
//

import UIKit

class BS_NavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        //拖拽返回上个页面
        let target = self.interactivePopGestureRecognizer?.delegate
        let pan:UIPanGestureRecognizer = UIPanGestureRecognizer.init(target: target!, action: Selector(("handleNavigationTransition:")))
        self.view.addGestureRecognizer(pan)
        
        //同时禁用系统原先的侧滑返回功能
        self.interactivePopGestureRecognizer?.isEnabled = false
        pan.delegate = (self as! UIGestureRecognizerDelegate)
        
     
        //self.navigationController?.interactivePopGestureRecognizer!.isEnabled = false
   //self.interactivePopGestureRecognizer?.delegate = nil;
    }
 
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if self.childViewControllers.count == 1 {
            
             return  false
            
        } else{
            
        return self.childViewControllers.count > 1
            
        }
        
    }
    
    //设置导航栏
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if childViewControllers.count > 0 {
            let button = UIButton(type: .custom)
            button.setTitle("返回", for: .normal)
            button.setImage(UIImage(named:"navigationButtonReturn"), for: .normal)
            button.setImage(UIImage(named:"navigationButtonReturnClick"), for: .highlighted)
            button.addTarget(self, action: #selector(back), for: .touchUpInside)
            button.setTitleColor(UIColor.darkGray, for: .normal)
            button.setTitleColor(UIColor.red, for: .highlighted)
            button.sizeToFit()
            button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0)
            button.contentHorizontalAlignment = .left
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
            
            // 隐藏tabbar
            viewController.hidesBottomBarWhenPushed = true
            
        }
        super.pushViewController(viewController, animated: animated)
        
    }
    
}
extension BS_NavigationController {
    @objc func back() {
        popViewController(animated: true)
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
